// pages/noticeList/noticeList.js
var request = require('../../operation/operation.js')
var getNoticesOperation = require('../../operation/getNoticesOperation.js')
var util = require('../../utils/util.js')
var klassModel = require('../../models/klass.js')
var userModel = require('../../models/user.js')
var bxt = require('../../utils/bxt.js')
let noticeModel = require('../../models/notice.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    notices:null,
    klasses: null,
    klass:null,     // 当前班级
    filterCondition: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initClassView()
    this.getNotices()

    this.setData({
      filterCondition: noticeModel.getFilterCondition()
    })

    getApp().notificationCenter.register(bxt.REFRESH_NOTICES_MESSAGE, this, "getNotices");
    getApp().notificationCenter.register(bxt.RECEIPT_SUCCESSED_MESSAGE, this, "getNotices");
    getApp().notificationCenter.register(bxt.REVIEW_FAIL_MESSAGE, this, "getNotices");   
    getApp().notificationCenter.register(bxt.DELETE_REVIEW_MESSAGE, this, "getNotices");
    getApp().notificationCenter.register(bxt.REVIEW_PASS_MESSAGE, this, "getNotices");
    getApp().notificationCenter.register(bxt.DELETE_KLASS_MESSAGE, this, "handleDeleteKlass")
    getApp().notificationCenter.register(bxt.DELETE_NOTICE_MESSAGE, this, "getNotices");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().globalData.currentPage = '../noticeList/noticeList'
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {  
    getApp().notificationCenter.remove(bxt.REFRESH_NOTICES_MESSAGE, this) 
    getApp().notificationCenter.remove(bxt.RECEIPT_SUCCESSED_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.REVIEW_FAIL_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.DELETE_REVIEW_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.REVIEW_PASS_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.DELETE_KLASS_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.DELETE_NOTICE_MESSAGE, this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getNotices()
  },

  // onShareAppMessage: function (options) {
  //   return noticeModel.showActivateClass(this,options)
  // },

  /**
   * 单击切换班级处理事件
   */
  onSwitchClass:function() {
    // wx.navigateTo({
    //   url: '../switchClass/switchClass'        
    // })
    let klasses = [klassModel.getDefaultAllKlassItem()], tmpAllKlasses = klassModel.getAllKlassUIData()
    if (tmpAllKlasses) {
      klasses = klasses.concat(tmpAllKlasses)
    }
    let klassesString = []
    for (let index = 0, length = klasses.length; index < length; index++) {
      if ('0' == klasses[index].sid) {
        klassesString.push(klasses[index].klassName)
      }else {
        klassesString.push(klasses[index].schoolName + ' · ' + klasses[index].klassName)
      }
      
    }

    wx.showActionSheet({
      itemList: klassesString,      
      success: function(res) {
        klassModel.setShowNoticeKlass(klasses[res.tapIndex])
        wx.reLaunch({
          url: '../noticeList/noticeList',
        })
      }  
    })
  },

  onFilterNotice:function() {
    let conditions = noticeModel.NOTICE_FILTER_CONDITION,conditionsString = [] , that = this
    for (let index = 0, length = conditions.length; index < length; index++) {
      conditionsString.push(conditions[index].menuText)
    }

    wx.showActionSheet({
      itemList: conditionsString,
      success: function (res) {
        noticeModel.setFilterCondition(conditions[res.tapIndex])
        that.setData({
          filterCondition:conditions[res.tapIndex]
        })
        wx.reLaunch({
          url: '../noticeList/noticeList',
        })
      }
    })
  },
  
  onShowEditMessageView:function() {
    getApp().globalData.param = null  // 避免进入创建通知界面，误认为是修改模式
    wx.navigateTo({
      url: '../editNotice/editNotice'
    })
  },

  /**
   * 初始化界面班级信息
   */
  initClassView:function() {
    var klass = klassModel.getShowNoticeKlass()   
    this.setData({
      klass:klass
    })
  },

  handleDeleteKlass:function() {
    this.initClassView()
    this.getNotices()
  },

  /**
   * 获取班级通知列表数据
   */
  getNotices:function() {
    let that = this
    // role = userModel.getRole()
    getNoticesOperation.getNotices('received')
    .then(res => {
      that.setData({
        notices: that.makeUINotices(that.filterNotices(res))
        // role:role
      })
    })
  },

  /**
   * 将获取到的数据，转换成UI效果格式的数据
   */
  makeUINotices:function(notices) {
    if (null != notices) {
      var length = notices.length
      for (var index = 0; index < length; index++) {
        if (notices[index].createdAt) {
          notices[index].uiCreatedAt = util.formatDateTime(notices[index].createdAt)  // 为了界面时间按设计显示，引入uiCreatedAt属性
        }

        let nums = notices[index].receiptStatus[0].currentStatus.split('/');
        // 检查nums数组项数目，如果个人是3个，说明存在不通过的签收，按没有完成处理
        if (3 == nums.length) {
          notices[index].receiptStatus[0].finished = false
        }else {
          let status = nums[0] / nums[1];
          notices[index].receiptStatus[0].finished = (status == 1)
        }        
      }
    }
    return notices;
  },

  /**
   * 点击通知Cell事件处理函数
   */
  showMessageDetail:function(event) {
    getApp().globalData.param = event.currentTarget.dataset.notice;
    wx.navigateTo({
      url: '../noticeDetail/noticeDetail',
    })
  },

  /**
   * 过滤通知数据,只显示当前要看的班级
   */
  filterNotices:function(notices) {
    let currentClass = klassModel.getShowNoticeKlass()
    let noticeFilterCondition = noticeModel.getFilterCondition()
    let results = []
    for (let index = 0, length = notices.length; index < length; index++) {
      notices[index].klassInfo.role = userModel.getRole(notices[index].klassInfo)

      if ('0' != currentClass.sid && '1' == noticeFilterCondition.sid) {
        if (notices[index].klassInfo.sid == currentClass.sid) {
          results.push(notices[index])
        }
      } else if ('0' != currentClass.sid && '2' == noticeFilterCondition.sid) {        
        if (notices[index].klassInfo.sid == currentClass.sid && notices[index].klassInfo.role.role > userModel.HEAD_TEACHER && 0 == notices[index].myReceipts.length) {
          results.push(notices[index])
        }
      } else if ('0' == currentClass.sid && '2' == noticeFilterCondition.sid) {        
        if (notices[index].klassInfo.role.role > userModel.HEAD_TEACHER && 0 == notices[index].myReceipts.length) {          
          results.push(notices[index])
        }
      }else {
        results.push(notices[index])
      }
    }    
    
    return results
  },

  /**
   * 设置通知所属班级下的用户角色
   */
  setNoticesKlassRole:function(notices) {
    for (let index = 0, length = notices.length; index < length; index++) {
      notices[index].klassInfo.role = userModel.getRole(notices[index].klassInfo)
    }

    return notices
  }
})